home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / qtshell / application files / comapplication.c next >
Encoding:
Text File  |  2000-09-28  |  11.7 KB  |  461 lines

  1. //////////
  2. //
  3. //    File:        ComApplication.c
  4. //
  5. //    Contains:    Application-specific code for basic QuickTime movie display and control.
  6. //                This file is used for BOTH MacOS and Windows.
  7. //
  8. //    Written by:    Tim Monroe
  9. //                Based (heavily!) on the MovieShell code written by Apple DTS.
  10. //
  11. //    Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved.
  12. //
  13. //    Change History (most recent first):
  14. //
  15. //       <11>         12/16/98    rtm        removed all QTVR API calls, so we can remove QTVR.lib from project
  16. //       <10>         10/23/97    rtm        moved InitializeQTVR to InitApplication, TerminateQTVR to StopApplication
  17. //       <9>         10/13/97    rtm        reworked HandleApplicationMenu to use menu identifiers
  18. //       <8>         09/11/97    rtm        merged MacApplication.c and WinApplication.c into ComApplication.c
  19. //       <7>         08/21/97    rtm        first file for Windows; based on MacApplication.c for Mac sample code
  20. //       <6>         06/04/97    rtm        removed call to QTVRUtils_IsQTVRMovie in InitApplicationWindowObject
  21. //       <5>         02/06/97    rtm        fixed window resizing code
  22. //       <4>         12/05/96    rtm        added hooks into MacFramework.c: StopApplication, InitApplicationWindowObject
  23. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  24. //       <2>         11/27/96    rtm        conversion to personal coding style; added preliminary QTVR support
  25. //       <1>         12/21/94    khs        first file
  26. //       
  27. //
  28. //    QTShell is a simple QuickTime viewer framework. It demonstrates how to incorporate 
  29. //    QuickTime and QuickTime VR movies into an application. QTShell is based heavily on the 
  30. //    MovieShell QuickTime playback framework included with the DTS QT sample code.
  31. //
  32. //////////
  33.  
  34. // header files
  35. #include "ComApplication.h"
  36.   
  37. // global variables for Macintosh code
  38. #if TARGET_OS_MAC
  39. #endif
  40.  
  41. // global variables for Windows code
  42. #if TARGET_OS_WIN32
  43. extern HWND                ghWnd;                                    // the MDI frame window; this window has the menu bar
  44. extern int                gNumWindowsOpen;
  45. extern LPSTR            gCmdLine;
  46. #endif
  47.  
  48. long                    gMaxMilliSecToUse = 0L;
  49.  
  50.  
  51. //////////
  52. //
  53. // InitApplication
  54. // Do any application-specific initialization.
  55. //
  56. // The theStartPhase parameter determines which "phase" of application start-up is executed,
  57. // *before* the MDI frame window is created or *after*. This distinction is relevant only on
  58. // Windows, so on MacOS, you should always use kInitAppPhase_BothPhases.
  59. //
  60. //////////
  61.  
  62. void InitApplication (UInt32 theStartPhase)
  63. {
  64.     // ***do any start-up activities that should occur before the MDI frame window is created
  65.     if (theStartPhase & kInitAppPhase_BeforeCreateFrameWindow) {
  66.  
  67.     }    // end of kInitAppPhase_BeforeCreateFrameWindow
  68.  
  69.     // ***do any start-up activities that should occur after the MDI frame window is created
  70.     if (theStartPhase & kInitAppPhase_AfterCreateFrameWindow) {
  71. #if TARGET_OS_WIN32
  72.         // on Windows, open as movie documents any files specified on the command line
  73.         SendMessage(ghWnd, WM_OPENDROPPEDFILES, 0L, 0L);
  74. #endif
  75.     }    // end of kInitAppPhase_AfterCreateFrameWindow
  76. }
  77.  
  78.  
  79. //////////
  80. //
  81. // StopApplication
  82. // Do any application-specific shut-down.
  83. //
  84. // The theStopPhase parameter determines which "phase" of application shut-down is executed,
  85. // *before* any open movie windows are destroyed or *after*.
  86. //
  87. //////////
  88.  
  89. void StopApplication (UInt32 theStopPhase)
  90. {
  91.     // @@@INSERT APPLICATION-SPECIFIC SHUT-DOWN FUNCTIONALITY HERE
  92.     
  93.     // do any shut-down activities that should occur after the movie windows are destroyed
  94.     if (theStopPhase & kStopAppPhase_AfterDestroyWindows) {
  95.         
  96.     }    // end of kStopAppPhase_AfterDestroyWindows
  97. }
  98.  
  99.  
  100. //////////
  101. //
  102. // DoIdle
  103. // Do any processing that can/should occur at idle time.
  104. //
  105. //////////
  106.  
  107. void DoIdle (WindowReference theWindow)
  108. {
  109.     WindowObject         myWindowObject = NULL;
  110.     GrafPtr             mySavedPort;
  111.     
  112.     GetPort(&mySavedPort);
  113.     MacSetPort(GetPortFromWindowReference(theWindow));
  114.     
  115.     myWindowObject = GetWindowObjectFromWindow(theWindow);
  116.     if (myWindowObject != NULL) {
  117.         MovieController        myMC = NULL;
  118.     
  119.         myMC = (**myWindowObject).fController;
  120.         if (myMC != NULL) {
  121.  
  122. #if TARGET_OS_MAC
  123.             // restore the cursor to the arrow
  124.             // if it's outside the front movie window or outside the window's visible region
  125.             if (theWindow == GetFrontMovieWindow()) {
  126.                 Rect    myRect;
  127.                 Point    myPoint;
  128.                 
  129.                 GetMouse(&myPoint);
  130.                 MCGetControllerBoundsRect(myMC, &myRect);
  131.                 if (!MacPtInRect(myPoint, &myRect) || !PtInRgn(myPoint, GetPortFromWindowReference(theWindow)->visRgn))
  132.                     MacSetCursor(&qd.arrow);
  133.             }
  134. #endif    // TARGET_OS_MAC
  135.         }
  136.     }
  137.     
  138.     // @@@INSERT APPLICATION-SPECIFIC IDLE-TIME FUNCTIONALITY HERE
  139.     
  140.     MacSetPort(mySavedPort);
  141. }
  142.  
  143.  
  144. //////////
  145. //
  146. // DoUpdateWindow
  147. // Update the specified window.
  148. //
  149. //////////
  150.  
  151. void DoUpdateWindow (WindowReference theWindow, Rect *theRefreshArea)
  152. {
  153. #pragma unused(theRefreshArea)
  154.  
  155.     GrafPtr             mySavedPort;
  156.     
  157.     GetPort(&mySavedPort);
  158.     MacSetPort(GetPortFromWindowReference(theWindow));
  159.     
  160.     BeginUpdate(GetPortFromWindowReference(theWindow));
  161.     //EraseRect(theRefreshArea);        // this is important, for non-rectangular movies
  162.     
  163.     // @@@INSERT APPLICATION-SPECIFIC DRAWING FUNCTIONALITY HERE
  164.     
  165.     // draw the movie controller and its movie
  166.     MCDoAction(GetMCFromWindow(theWindow), mcActionDraw, theWindow);
  167.     
  168.     EndUpdate(GetPortFromWindowReference(theWindow));
  169.     MacSetPort(mySavedPort);
  170. }
  171.  
  172.  
  173. //////////
  174. //
  175. // HandleContentClick
  176. // Handle mouse button clicks in the specified window.
  177. //
  178. //////////
  179.  
  180. void HandleContentClick (WindowReference theWindow, EventRecord *theEvent)
  181. {
  182. #pragma unused(theEvent)
  183.  
  184.     GrafPtr             mySavedPort;
  185.     
  186.     GetPort(&mySavedPort);
  187.     MacSetPort(GetPortFromWindowReference(theWindow));
  188.     
  189.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  190.  
  191.     MacSetPort(mySavedPort);
  192. }
  193.  
  194.  
  195. //////////
  196. //
  197. // HandleApplicationKeyPress
  198. // Handle application-specific key presses.
  199. // Returns true if the key press was handled, false otherwise.
  200. //
  201. //////////
  202.  
  203. Boolean HandleApplicationKeyPress (char theCharCode)
  204. {
  205.     Boolean        isHandled = true;
  206.     
  207.     switch (theCharCode) {
  208.     
  209.         // @@@HANDLE APPLICATION-SPECIFIC KEY PRESSES HERE
  210.  
  211.         default:
  212.             isHandled = false;
  213.             break;
  214.     }
  215.  
  216.     return(isHandled);
  217. }
  218.  
  219.  
  220. #if TARGET_OS_MAC
  221. //////////
  222. //
  223. // CreateMovieWindow
  224. // Create a window to display a movie in.
  225. //
  226. //////////
  227.  
  228. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  229. {
  230.     WindowRef            myWindow;
  231.     
  232.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr)-1L, true, 0);
  233.     return(myWindow);
  234. }
  235. #endif
  236.  
  237.  
  238. //////////
  239. //
  240. // HandleApplicationMenu
  241. // Handle selections in the application's menus.
  242. //
  243. // The theMenuItem parameter is a UInt16 version of the Windows "menu item identifier". 
  244. // When called from Windows, theMenuItem is simply the menu item identifier passed to the window proc.
  245. // When called from MacOS, theMenuItem is constructed like this:
  246. //     *high-order 8 bits == the Macintosh menu ID (1 thru 256)
  247. //     *low-order 8 bits == the Macintosh menu item (sequential from 1 to ordinal of last menu item in menu)
  248. // In this way, we can simplify the menu-handling code. There are, however, some limitations,
  249. // mainly that the menu item identifiers on Windows must be derived from the Mac values. 
  250. //
  251. //////////
  252.  
  253. void HandleApplicationMenu (UInt16 theMenuItem)
  254. {
  255.     WindowObject        myWindowObject = NULL;
  256.     MovieController     myMC = NULL;
  257.     Movie                 myMovie = NULL;
  258.     
  259.     myWindowObject = GetWindowObjectFromFrontWindow();
  260.     if (myWindowObject != NULL) {
  261.         myMC = (**myWindowObject).fController;
  262.         myMovie = (**myWindowObject).fMovie;
  263.     }
  264.     
  265.     // make sure we have a valid movie controller
  266.     if (myMC == NULL)
  267.         return;
  268.  
  269.     switch (theMenuItem) {
  270.         case IDM_CONTROLLER:
  271.             QTUtils_ToggleControllerBar(myMC);
  272.             break;
  273.  
  274.         case IDM_SPEAKER_BUTTON:
  275.             QTUtils_ToggleControllerButton(myMC, kQTVRSpeakerButton);
  276.             break;
  277.             
  278.         default:
  279.             break;
  280.     }    // switch (theMenuItem)
  281. }
  282.  
  283.  
  284. //////////
  285. //
  286. // AdjustApplicationMenus
  287. // Adjust state of items in the application's menus.
  288. //
  289. // Currently, the Mac application has only one app-specific menu ("Test"); you could change that.
  290. //
  291. //////////
  292.  
  293. void AdjustApplicationMenus (WindowReference theWindow, MenuReference theMenu)
  294. {
  295.     WindowObject        myWindowObject = NULL; 
  296.     MovieController     myMC = NULL;
  297.     MenuReference        myMenu;
  298.     
  299. #if TARGET_OS_WIN32
  300.     myMenu = theMenu;
  301. #elif TARGET_OS_MAC
  302.     myMenu = GetMenuHandle(kTestMenu);
  303. #endif
  304.     
  305.     if (theWindow != NULL)
  306.         myWindowObject = GetWindowObjectFromWindow(theWindow);
  307.  
  308.     if (myWindowObject != NULL)
  309.         myMC = (**myWindowObject).fController;
  310.  
  311.     if (myMC == NULL) {
  312.         // no movie controller, so disable all the Test menu items
  313.         SetMenuItemState(myMenu, IDM_CONTROLLER, kDisableMenuItem);
  314.         SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kDisableMenuItem);
  315.     } else {
  316.         // we've got a movie controller, so it's safe to proceed....
  317.         SetMenuItemState(myMenu, IDM_CONTROLLER, kEnableMenuItem);
  318.  
  319.         // if controller bar is visible, enable Test menu items
  320.         if (QTUtils_IsControllerBarVisible(myMC)) {
  321.             SetMenuItemLabel(myMenu, IDM_CONTROLLER, "Hide &Controller");
  322.                // ungray the other menu items
  323.             SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kEnableMenuItem);
  324.             
  325.             // handle Test menu items
  326.             if (QTUtils_IsControllerButtonVisible(myMC, kQTVRSpeakerButton))
  327.                 SetMenuItemLabel(myMenu, IDM_SPEAKER_BUTTON, "Hide Spea&ker Button");
  328.             else 
  329.                 SetMenuItemLabel(myMenu, IDM_SPEAKER_BUTTON, "Show Spea&ker Button");
  330.             
  331.         } else {
  332.             // controller bar is not visible
  333.             
  334.             SetMenuItemLabel(myMenu, IDM_CONTROLLER, "Show &Controller");
  335.               // gray the other menu items
  336.             SetMenuItemState(myMenu, IDM_SPEAKER_BUTTON, kDisableMenuItem);
  337.         }
  338.     }
  339. }
  340.  
  341.  
  342. //////////
  343. //
  344. // DoApplicationEventLoopAction
  345. // Perform any application-specific event loop actions.
  346. //
  347. // Return true to indicate that we've completely handled the event here, false otherwise.
  348. //
  349. //////////
  350.  
  351. Boolean DoApplicationEventLoopAction (EventRecord *theEvent)
  352. {
  353. #pragma unused(theEvent)
  354.     
  355.     return(false);            // no-op for now
  356. }
  357.  
  358.  
  359. //////////
  360. //
  361. // AddControllerFunctionality
  362. // Configure the movie controller.
  363. //
  364. //////////
  365.  
  366. void AddControllerFunctionality (MovieController theMC)
  367. {
  368.     long            myControllerFlags;
  369.     
  370.     // CLUT table use
  371.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  372.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  373.  
  374.     // enable keyboard event handling
  375.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  376.     
  377.     // disable drag support
  378.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  379. }
  380.  
  381.  
  382. //////////
  383. //
  384. // InitApplicationWindowObject
  385. // Do any application-specific initialization of the window object.
  386. //
  387. //////////
  388.  
  389. void InitApplicationWindowObject (WindowObject theWindowObject)
  390. {
  391. #pragma unused(theWindowObject)
  392.  
  393.     // @@@INSERT APPLICATION-SPECIFIC WINDOW OBJECT START-UP HERE
  394. }
  395.  
  396.  
  397. //////////
  398. //
  399. // RemoveApplicationWindowObject
  400. // Do any application-specific clean-up of the window object.
  401. //
  402. //////////
  403.  
  404. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  405. {
  406. #pragma unused(theWindowObject)
  407.     
  408.     // @@@INSERT APPLICATION-SPECIFIC WINDOW OBJECT CLEAN-UP HERE
  409.  
  410.     // DoDestroyMovieWindow in MacFramework.c or MovieWndProc in WinFramework.c
  411.     // releases the window object itself
  412. }
  413.  
  414.  
  415. //////////
  416. //
  417. // ApplicationMCActionFilterProc 
  418. // Intercept some mc actions for the movie controller.
  419. //
  420. // NOTE: The theRefCon parameter is a handle to a window object record.
  421. //
  422. //////////
  423.  
  424. PASCAL_RTN Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, long theRefCon)
  425. {
  426. #pragma unused(theMC, theParams)
  427.  
  428.     Boolean                isHandled = false;            // false => allow controller to process the action
  429.     WindowObject        myWindowObject = NULL;
  430.     
  431.     myWindowObject = (WindowObject)theRefCon;
  432.     if (myWindowObject == NULL)
  433.         return(isHandled);
  434.         
  435.     switch (theAction) {
  436.     
  437.         // handle window resizing
  438.         case mcActionControllerSizeChanged:
  439.             SizeWindowToMovie(myWindowObject);
  440.             break;
  441.  
  442.         // handle idle events
  443.         case mcActionIdle:
  444.             //DoIdle((**myWindowObject).fWindow);
  445.             break;
  446.             
  447.         // handle movie edits
  448.         case mcActionMovieEdited:
  449.         
  450.             break;
  451.             
  452.         default:
  453.             break;
  454.             
  455.     }    // switch (theAction)
  456.     
  457.     return(isHandled);    
  458. }
  459.  
  460.  
  461.